<template>
	<view class='head'>
		<view class="bgi">
			<image src="http://fsl-test.dderp.cn:33548/adh-mini/index/bgimage.png" alt=""></image>
		</view>

		<TopNav :show="show2" :close_show='false' :home_show='false'>
			<template v-slot:order>
				<view class="order" style="margin-left:140rpx;">
					<view class="share">
						<button class="btn" open-type="share">
							<image src="/static/images/my/fx.png" mode=""></image>
							邀请
						</button>
					</view>
					<view class="" @click="openBusinessType" style="font-size: 25rpx;">
						<image src="http://fsl-test.dderp.cn:33548/adh-mini/cart/qh.png" mode=""></image>
						切换
					</view>
					<view class="" @click="exit" style="font-size: 25rpx;margin-left:30rpx;">
						退出
					</view>
				</view>
			</template>
		</TopNav>
		<view class="contenr padding">
			<view class="shop_del padding radius">
				<view class="shop_title">
					{{ state.shop_info.Brand_ChnName ? state.shop_info.Brand_ChnName : state.shop_info.brand_chnname }}
				</view>
				<!-- <view class="shop_info">最近 KY301 在搞促销活动，满5箱送1箱，8月11日到期，速速进货</view> -->
				<view class="shop_address">
					<view class="a">
						地址：{{ state.shop_info.brand_address }}
					</view>
					<view class="b">
						<view @click="makePhoneCall">
							<image src="http://fsl-test.dderp.cn:33548/adh-mini/index/dh.png" alt=""></image>
							<text>电话</text>
						</view>
						<view>
							<image src="http://fsl-test.dderp.cn:33548/adh-mini/index/dz.png" alt=""></image>
							<text>地址</text>
						</view>
					</view>
				</view>
				<view class="gg">
					<!-- 最近 KY301 在搞促销活动，满5箱送1箱，8月11日到期，速速进货 -->
				</view>
			</view>
			<view class="way_item">
				<view class="way padding radius">
					<view class="way_letf" @click="toPage('快速进货')">
						<image src="http://fsl-test.dderp.cn:33548/adh-mini/index/ps.png" alt=""></image>
						<view class="way_title">
							快速进货
						</view>
						<view class="way_lat">
							一键下单，快捷方便
						</view>
					</view>
					<view class="way_right" @click="toPage('订单查询')">
						<image src="http://fsl-test.dderp.cn:33548/adh-mini/index/qd.png" alt=""></image>
						<view class="way_title">
							订单查询
						</view>
						<view class="way_lat">
							查订单，查轨迹
						</view>
					</view>
				</view>
				<!-- @click="toPage('对账单')" -->
				<view class="dz" @click="toPage('对账单')">
					<image src="http://fsl-test.dderp.cn:33548/adh-mini/index/qrzd.png" alt=""></image>
					对账单
				</view>
			</view>

			<!-- <view class="ye ">
			    <image src="http://fsl-test.dderp.cn:33548/adh-mini/index/ye.png" class="radius" style="opacity: 1;" alt="">
			    </image>
			    <view class="ye_contenr padding">
			        <view class="ye_top_contenr">
			            <view class="ye_letf">
			                <view class="a" style="color: #555;">账上余额</view>
			                <view class="b" style="color: #555;">99999.88</view>
			                <view class="c" style="color: #555;">年度统计：300.00/300.00</view>
			            </view>
			            <view class="ye_right">
			                <view class="a"> A <text>级</text> </view>
			                <view class="b">目前排322名</view>
			            </view>
			        </view>
			        <view class="ye_bottom_title">
			            <view class="a">考核任务:￥8888.00 </view>
			            <view class="b">已完成:￥8888.00 </view>
			            <view class="c">完成率:任务未下达 </view>
			        </view>
			    </view>
			</view> -->
			<view class="user_ye">
				<image src="http://fsl-test.dderp.cn:33548/adh-mini/index/ye.png" class="radius" style="opacity: 1;" alt="">
				</image>
				<view class="bottom_discount" v-if="state.user_balance && state.user_balance.qyzk == 10">
					不打折
				</view>
				<view class="bottom_contenr">
					累计拿货8000元，再拿200元即可享2.5折优惠...
				</view>
				<view class="ye_contenr">
					<view class="top_contenr">
						<view class="user_price" @click="toPage('对账单')">
							<view class="num">
								{{ state.user_balance.agent_amount }}
							</view>
							<view class="text">
								余额(元)
							</view>
						</view>
						<view class="user_coupon" @click="toPage('代金券')">
							<view class="num">
								{{ state.user_balance.voucher_num }}
							</view>
							<view class="text">
								代金券
							</view>
						</view>
						<view class="user_integral" @click="toPage('积分')">
							<view class="num">
								{{ state.user_balance.jf_amount }}
							</view>
							<view class="text">
								积分
							</view>
						</view>
					</view>
				</view>

			</view>
			<VipDom></VipDom>
			<!-- 储值活动 -->
			<!-- <view class="store radius padding">
				<view class="store_title">
					储值活动
				</view>
				<view class="store_list">
					<view class="list_item" v-for="(item, index) in 10" :key="index">
						<view class="item_top">
							<view class="c">
								充值 <text>{{ 100 * (index + 1) }}</text> 元
							</view>
							<view class="s">
								送 <text>{{ 10 * (index + 1) }}</text> 元
							</view>
						</view>
						<view class="item_bottom">
							<view class="">
								立即储值
							</view>
						</view>
						<view class="recommend" v-if="index == 1">
							推荐
						</view>
					</view>
				</view>
			</view> -->

			<!-- 其他门店 -->
			<!--  <view class="qt_mdian">
                <nut-fixed-nav :position="{ bottom: '70px' }" v-model:visible="visible" :nav-list="navList"
                    un-active-text='其他门店' active-text='收起门店' />
            </view> -->
			<view class="banner">
				<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
					<swiper-item v-for="(item, index) in 3" :key="index">
						<view class="swiper-item">
							<image :src="`http://fsl-test.dderp.cn:33548/adh-mini/index/banner${index + 1}.png`" mode="">
							</image>
						</view>
					</swiper-item>
				</swiper>
			</view>
			<view class="" style="font-size: 25rpx ;color: #d1d1d1;text-align: center;line-height: 100rpx;">
				到底了!
			</view>
		</view>
		<nut-popup round :lock-scroll='true' z-index='9999' :close-on-click-overlay='false' position="center"
			v-model:visible="state.business_show">
			<view class="popup_business">
				<view class="business_title">
					选择商家
				</view>
				<view class="business_list">
					<view class="business_item" :id="state.shop_info.agent_code == item.agent_code ? 'check' : ''"
						@click="selectBusiness(item)" v-for="(item, index) in state.business_list" :key="index">
						<view class="item_img">
							<image v-if="item.brand_image" :src="item.brand_image" mode=""></image>
							<image v-else src="/static/images/my/noPictures.png" mode=""></image>
						</view>
						<view class="item_info">
							<view class="info_text_1">
								{{ item.brand_chnname ? item.brand_chnname : item.Brand_ChnName }}
							</view>
							<view class="info_text_2">
								{{ item.brand_address }}
							</view>
							<view class="info_text_3">
								最近进货：<text>{{ item.last_order_date }}</text>
							</view>
						</view>
						<view class="check" v-if="state.shop_info.agent_code == item.agent_code">
							✔
						</view>
					</view>
				</view>
				<view class="business_btn">
					<view class="btn" @click="submitBusiness">
						确认
					</view>
				</view>
			</view>
		</nut-popup>
	</view>
</template>
<script setup>
import {
	ref,
	reactive
} from 'vue'
import {
	onLaunch,
	onShow,
	onShareAppMessage,
	onLoad
} from '@dcloudio/uni-app'
import TopNav from '../../../../common/top_nav/index.vue'
import VipDom from '../../../../common/vip/index.vue'
import {
	Proname,
	switchBusiness
} from '../../../../api/api.js'
const show = ref(true)
const visible = ref(false);
const navList = reactive([{
	id: 1,
	text: '首页',
	icon: 'https://img11.360buyimg.com/imagetools/jfs/t1/117646/2/11112/1297/5ef83e95E81d77f05/daf8e3b1c81e3c98.png'
},
{
	id: 2,
	text: '分类',
	icon: 'https://img12.360buyimg.com/imagetools/jfs/t1/119490/8/9568/1798/5ef83e95E968c69a6/dd029326f7d5042e.png'
},
{
	id: 3,
	text: '购物车',
	num: 2,
	icon: 'https://img14.360buyimg.com/imagetools/jfs/t1/130725/4/3157/1704/5ef83e95Eb976644f/b36c6cfc1cc1a99d.png'
},
{
	id: 4,
	text: '我的',
	icon: 'https://img12.360buyimg.com/imagetools/jfs/t1/147573/29/1603/1721/5ef83e94E1393a678/5ddf1695ec989373.png'
}
]);

// 分享
onShareAppMessage((res) => {
	let list = uni.getStorageSync('this')
	var obj = {
		...list,
		user_name: state.value.user_info.XM
	}
	return {
		title: `${state.value.user_info.XM}邀请你进入${state.value.user_info.AGENT}拿货，价格优惠多多`,
		path: `/pages/login/login?item=${JSON.stringify(obj)}`,
		imageUrl: 'http://fsl-test.dderp.cn:33548/adh-mini/logo.png'
	}
})
const state = ref({
	shop_info: '',
	user_balance: '',
	user_info: '',
	business_show: false,
	business_list: []
})
onLoad((val) => {
	// state.value.shop_info = JSON.parse(val.item)
	getBusinessDetail()
	state.value.user_info = uni.getStorageSync('session')
})
const exit = () => {
	uni.setStorageSync('token','')
	uni.redirectTo({
		url: '/pages/login/login'
	})
}
// 拨打电话
const makePhoneCall = () => {
	uni.makePhoneCall({
		phoneNumber: state.value.shop_info.brand_tel
	});
}

// 获取商家列表
const openBusinessType = () => {
	Proname({
		proname: 'JJ0603',
		lxfs: state.value.user_info.SJ,
	}).then(res => {
		console.log(res);
		state.value.business_show = true
		state.value.business_list = res.rows
		console.log(state.value);
		if (state.value == '') {
			uni.showToast({
				title: '余额那些为空，所以请求不出来'
			})
		}
	})
}
// 同一个事件 前往方向不一样
const toPage = (val) => {
	switch (val) {
		case '快速进货':
			uni.navigateTo({
				url: '/pages/placeOrder/index'
			})
			break;
		case '订单查询':
			uni.navigateTo({
				url: '/pages/myOrder/index?index=0'
			})
			break;
		case '对账单':
			uni.navigateTo({
				url: '/pages/placeOrder/bill/index'
			})
			break;
		case '代金券':
			uni.navigateTo({
				url: '/pages/coupon/index'
			})
			break;
		case '积分':
			// uni.navigateTo({
			// 	url: '/pages/placeOrder/bill/index'
			// })
			break;
	}
}

// 选完商家后 触发
const without_code = async () => {
	let res = await switchBusiness({
		agent_code: state.value.shop_info.agent_code,
		tel: state.value.shop_info.tel,
	})
	state.value.agent_code = res.session[0]
	uni.setStorageSync('token', res.TOKEN)
	uni.setStorageSync('session', res.session[0])
	getBusinessDetail()
}
// 确认 商家
const submitBusiness = () => {
	// 存当前选中的商家
	uni.setStorageSync('this', state.value.shop_info)
	without_code()
	uni.reLaunch({
		url: '/pages/tabbar/index/shop/shop_list?t=' + new Date().getTime()
	})
}

// 获取用户余额
const getBusinessDetail = async () => {
	let tel = uni.getStorageSync('session')
	state.value.shop_info = uni.getStorageSync('this')
	let params = {
		proname: 'JJ0602',
		tel: tel.SJ,
		agent_code: tel.DWDM,
	}
	let res = await Proname(params)
	console.log(res);
	state.value.user_balance = res.rows[0]
	uni.setStorageSync('agent_code', state.value.shop_info.brand_name)
}
// 选择
const selectBusiness = (val) => {
	state.value.shop_info = val
}
</script>
<style scoped lang="scss">
@import '../../../../style/shop_list.scss';
</style>